<!-- studyCourseChapter.html -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no, email=no, date=no, address=no">
    <title>课程章节</title>
    <link rel="stylesheet" type="text/css" href="../../css/api.css"/>
    <!--<link rel="stylesheet" type="text/css" href="../../css/mui.min.css" />-->
    <link rel="stylesheet" type="text/css" href="../../css/weui.min.css"/>
    <link rel="stylesheet" href="../../css/base.css"/>
    <link rel="stylesheet" href="../../css/CFNetPP_Base.css">
    <style type="text/css">
        .chapter_cell_icon {
            width: 20px;
            height: 20px;
            background-size: 20px 20px;
        }

        .chapter_content_cell {
            padding-left: 30px;
        }

        .doc_unstudy {
            background-image: url(../../image/doc_icon.png);
        }

        .doc_studying {
            background-image: url(../../image/doc_icon_studying.png);
        }

        .doc_studied {
            background-image: url(../../image/doc_icon_studied.png);
        }

        .video_unstudy {
            background-image: url(../../image/video_icon.png);
        }

        .video_studying {
            background-image: url(../../image/video_icon_studying.png);
        }

        .video_studied {
            background-image: url(../../image/video_icon_studied.png);
        }

        .richtext_unstudy {
            background-image: url(../../image/richtext_unstudy.png);
        }

        .richtext_studying {
            background-image: url(../../image/richtext_studying.png);
        }

        .richtext_studied {
            background-image: url(../../image/richtext_studied.png);
        }

        .ziyuan_unstudy {
            background-image: url(../../image/ziyuan_icon.png);
        }

        .ziyuan_studying {
            background-image: url(../../image/ziyuan_icon_studying.png);
        }

        .ziyuan_studied {
            background-image: url(../../image/ziyuan_icon_studied.png);
        }

        .tests_untest {
            background-image: url(../../image/study_test.png);
        }

        .tests_tested {
            background-image: url(../../image/study_tested.png);
        }

        .taolun_normal {
            background-image: url(../../image/icon_taolun_normal.png);
        }

        .taolun_highlight {
            background-image: url(../../image/icon_taolun_highlight.png);
        }

        .downloaded {
            background-image: url(../../image/icon_download_finish.png);
        }

        .download_progress {
            background-color: rgba(4, 190, 2, 0.25);
            height: 100%;
            width: 0;
            position: absolute;
            left: 0;
            top: 0;
        }

        .chapter_title {
            font-size: 14px;
            padding-left: 10px;
            padding-right: 10px;
            color: #393939;
        }

        .base_style_text_color .chapter_title {
            color: #18b4ed;
        }

        .list_section_contnt {
            font-size: 16px;
            color: #454545;
            background-color: #f9f9f9;
        }

        .chapter_etime {
            margin-right: 10px;
            font-size: 14px;
        }

        .noSign {
            width: auto;
        }

        .loadFailure {
            width: auto;
        }

        .footer-view {
            position: fixed;
            left: 0;
            bottom: 0;
            margin: 0;
            width: 100%;
            height: 50px;
            padding: 0;
        }

        .footer-view .mui-btn-block {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
            background-color: #ff9900;
            border-color: #ff9900;
            color: white;
            font-size: 16px;
        }

        .footer-view .mui-btn-block:active {
            background-color: #ff9900;
        }

        .mui-btn, button {
            border-radius: 0px;
            border-top-left-radius: 0px;
            border-top-right-radius: 0px;
            border-bottom-right-radius: 0px;
            border-bottom-left-radius: 0px;
        }

        .blank-div {
            height: 50px;
        }

        .weui_panel_hd:after {
            border-bottom: 1px solid #ededed;
            color: #ededed;
            -webkit-transform: scaleY(.3);
            transform: scaleY(.3);
            left: 0;
        }

        .part-title {
            font-size: 14px;
            color: #666;
            line-height: 20px;
        }

        .part-cell {
            padding: 10px 15px;
        }

        .tag-cell {
            padding: 15px 15px;
        }
    </style>
</head>
<body>
<div class="noSign noDataDiv" hidden="hidden" id="noData">
    <img src="../../image/icon_img.png" width="60%">
    <p class="font16 top_15 base_dark_grey">
        暂无数据
    </p>
</div>
<div class="loadFailure noInternetDiv" hidden="hidden" id="noNet">
    <img src="../../image/icon_img01.png" width="60%">
    <p class="">
        数据加载失败
    </p>
    <p>
        请检查您的网络
    </p>
    <button type="button" class="mui-btn mui-btn-outlined" onclick="loadData()">
        重新加载
    </button>
</div>
<div class="weui_panel" style="margin-top: 0;">
    <div id="chapter_content">
        <!-- <p class='weui_cells_title'>第2节</p>
        <div class='weui_cell part-cell'>
            <div class='weui_cell_bd weui_cell_primary'>
                <p class='part-title'>第1节</p>
            </div>
        </div>
        <div class='chapter_content_cell weui_cell tests_cell base_style_text_color'>
            <p class='chapter_cell_icon tests_untest' alt=''><p>
            <div class='download_progress'></div>
            <div class='weui_cell_hd'><img src=''/></div>
            <div class='weui_cell_bd weui_cell_primary'>
                <label class='chapter_title'>呵呵呵呵呵</label>
            </div>
            <span class='chapter_etime'>10:10</span>
            <span class='weui_cell_ft chapter_cell_icon download_icon'></span>
        </div> -->
    </div>
</div>
<div class="blank-div" hidden="hidden"></div>
<div class="footer-view" hidden="hidden">
    <button id="study_btn" type="button" onclick="onExamClick()" class="mui-btn mui-btn-block"></button>
</div>
</body>
<script type="text/javascript" src="../../script/jquery.min.js"></script>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/MD5.js"></script>
<script type="text/javascript" src="../../script/requestTool.js"></script>
<script type="text/javascript" src="../../script/databaseOperation.js"></script>
<script type="text/javascript" src="../../script/study_courseTimeRecord.js"></script>
<script type="text/javascript" src="../../script/getToken.js"></script>
<script type="text/javascript">

    var onDocClick;
    var onVideoClick;

    var studyTimeRecord;

    var chapterParam;
    var chapterArr = new Array();
    var testArr = new Array();
    var richtextArr = new Array();
    var allChapters = new Array();
    var isSelected;
    var checks;
    var crid;
    var uid;
    var jid;
    var isFreeCourse = true;
    var open_type = '';
    //判断当前课程考核的状态
    var score_id = '';
    var score = '';
    var currentParam = null;//当前跳入的入参
    DBReady = function () {
        prepareData();
// 		SendBehaviorTrajectoriesNotifi('0', 'mooc章节列表', '008');
        chapterParam = api.pageParam;
        checks = chapterParam.courseChecks;
        isSelected = chapterParam.selected;
        if (chapterParam.isFreeCourse === false) {
            isFreeCourse = false;
        }
        crid = chapterParam.crid;
        loadData();
        setRefreshHeader();
        //监听课程考核成功状态
        api.addEventListener({
            name: 'RefreshCourseTestNotifi'
        }, function (ret, err) {
            setupExamBtn();
        });
        //监听是否添加课程
        api.addEventListener({
            name: 'selectedCourseNotifi'
        }, function (ret, err) {
            isSelected = ret.value.selected;
        });
        //监听文档返回按钮
        api.addEventListener({
            name: 'StudyWebBaiDuDocBackNotifi'
        }, function (ret, err) {
            var docParam = ret.value.studyParam;
            study_courseTimeRecordFun(docParam, 'NONE', 'true');
        });
        //监听视频返回按钮
        api.addEventListener({
            name: 'baiduWebVideoBackNotifi'
        }, function (ret, err) {

            if (ret.value.hasOwnProperty('videoParam')) {
                var videoParam = ret.value.videoParam;
                sendBuriedNotifi('1', 'mooc视频', '009', videoParam.vid);
                study_courseTimeRecordFun(videoParam, 'NONE', 'true');
            } else {
                sendBuriedNotifi('1', 'mooc视频', '009', "");
            }

        });
        //互动资源返回时调用
        api.addEventListener({
            name: 'moocCourseRecordNotifi'
        }, function (ret, err) {
            var param = ret.value;
            study_courseTimeRecordFun(param, 'NONE', 'true');
        });
        //视频播放完成时点击返回多调一次计时
        api.addEventListener({
            name: 'videoPlayFinishNotifi'
        }, function (ret, err) {
            api.sendEvent({
                name: 'moocChapterBackNotifi',
                extra: currentParam
            });
        });
        //页面即将显示
        api.addEventListener({
            name: 'viewappear'
        }, function (ret, err) {
            //页面即将显示时根据进度设置考试按钮
            loadStudyScheduleSetupExamBtn();
            if (currentParam != null) {
                //退出刷新进度，暂时屏蔽
                // api.sendEvent({
                //     name: 'moocChapterBackNotifi',
                //     extra: currentParam
                // });
                study_courseTimeRecordFun(currentParam, 'NONE', 'true');
                currentParam = null;
            }
        });
        // removeComment
        api.addEventListener({
            name: 'removeComment'
        }, function (ret, err) {
        });
        api.addEventListener({
            name: 'allChapterStudiedNotifi'
        }, function (ret, err) {
        });
        api.addEventListener({
            name: 'studyPassedExamNotifi'
        }, function (ret, err) {
        });
        // loadCommentListData
        api.addEventListener({
            name: 'loadCommentListData'
        }, function (ret, err) {

        });
        api.addEventListener({
            name: 'chapterUpdateNotifi'
        }, function (ret, err) {
            loadData();
        });
        api.addEventListener({
            name: 'closewindow'
        }, function (ret, err) {
            clearInterval(studyTimeRecord);
        });
        api.addEventListener({
            name: 'studyTestSucceedNotifi'
        }, function (ret, err) {
            $('#' + ret.value.chapterid).children('p:first').addClass('tests_tested');
        });
        api.addEventListener({
            name: 'testDownloadFinished'
        }, function (ret, err) {
            var chapterid = ret.value.vid;
            $('#' + chapterid).find('.download_icon').addClass('downloaded');
        });
        api.addEventListener({
            name: 'changeCrid'
        }, function (ret, err) {
            crid = ret.value.crid;
        });
        //监听开始下载按钮
        api.addEventListener({
            name: 'startDownloadCourse'
        }, function (ret, err) {
            if (chapterArr == undefined || chapterArr == null || chapterArr == '' || chapterArr.length == 0) {
                api.toast({
                    msg: '图文课程无需下载'
                });
                api.sendEvent({
                    name: 'cancelDownloadFinish',
                    extra: {courseid: chapterParam.courseid}
                });
                return;
            }
            openDB(function (ret, err) {
                var sql = "UPDATE t_study_course_info SET iscache = '2' WHERE courseid = '" + chapterParam.courseid + "';";
                executeSQL(sql, function (ret, err) {
                    if (ret.status) {

                    } else {

                    }
                });
            });
            api.sendEvent({
                name: 'downloadFiles',
                extra: {
                    fileDatas: chapterArr,
                    testDatas: testArr
                }
            });
            setRichtextsDownloadStatus();
        });
        //监听开始学习
        api.addEventListener({
            name: 'startStudy'
        }, function (ret, err) {
            // alert('点击进入学习');
            if (allChapters != null && allChapters != undefined && allChapters != '' && allChapters.length != 0) {
                // alert('点击进入学习'+JSON.stringify(allChapters));
                var mk = 0;
                var vids = [];
                for (var i = 0; i < allChapters.length; i++) {
                    //点进入学习进第一节课
                    var vid = allChapters[i].id;
                    selectContentType(vid, function (current_vid, chaptertag) {
                        selectStudyStatus(current_vid, chaptertag, function (chapter_id, status, chapter_content_type) {
                            mk++;
                            if (status != 1) {
                                vids.push(chapter_id);
                            }
                            if (mk == allChapters.length) {
                                // alert(mk+'点击进入学习'+JSON.stringify(vids));
                                if (vids != null && vids != undefined && vids != '' && vids.length != 0) {
                                    var vvid = vids[0];
                                    var chapterID = '#' + vvid;
                                    $(chapterID).trigger("click");
                                    // alert(mk+'点击进入学习'+JSON.stringify(vvid));
                                    //设置正在学习状态
                                    $(chapterID).children('p:first').addClass('doc_studying');
                                    startStudying(vvid);
                                } else {
                                    var connectionType = api.connectionType;
                                    if (connectionType == 'none') {
                                        // api.toast({
                                        //                    	msg:'网络异常，请检查网络',
                                        //                       duration: 2000,
                                        //     location: 'bottom'
                                        //                    	});
                                    } else {
                                        var vvid = allChapters[0].id;
                                        var chapterID = '#' + vvid;
                                        $(chapterID).trigger("click");
                                    }
                                }
                            }
                        });
                    });
                }
            } else {
                api.toast({
                    msg: '该课程没有内容，无法学习，换个其他课程吧!'
                });
            }
        });
        //监听取消下载按钮
        api.addEventListener({
            name: 'cancelDownloadCourse'
        }, function (ret, err) {
            if (chapterArr == undefined || chapterArr == null || chapterArr == '' || chapterArr.length == 0) {
                return;
            }
            api.sendEvent({
                name: 'cancelDownloadFiles',
                extra: {
                    fileDatas: chapterArr
                }
            });
            cancelDownloadTestAndRich();
        });
        //监听取消下载完成
        api.addEventListener({
            name: 'cancelDownloadFinish'
        }, function (ret, err) {
            var courseid = ret.value.courseid;
            // openDB(function(ret, err){
            // 	var sql = "UPDATE t_study_course SET iscache = '0' WHERE courseid = '" + courseid + "';";
            // executeSQL(sql, function(ret, err){
            // 				if (ret.status) {
            // 					//删除章节缓存地址
            // 		var updateChapterSql = "UPDATE t_study_chapter SET localpath = null WHERE cid=" + courseid + ";";
            // 		executeSQL(updateChapterSql, function(ret, err) {

            // 		});

            // 				}else{

            // 	}
            // });
            // });
            var fs = api.require('fs');
            openDB(function (ret, err) {
                if (ret.status) {
                    var sql = "SELECT cachepath FROM t_study_course_info WHERE courseid=" + courseid;
                    selectSQL(sql, function (ret, err) {
                        if (ret.status) {
                            var filepath = ret.data[0].cachepath;
                            fs.rmdir({
                                path: filepath
                            }, function (ret, err) {
                                if (ret.status) {
                                    //更新t_study_course中iscache为0
                                    var sql = "UPDATE t_study_course_info SET iscache = '0' where courseid=" + courseid + ";";
                                    executeSQL(sql, function (ret, err) {
                                        //删除章节缓存地址
                                        var updateChapterSql = "UPDATE t_study_chapter SET localpath = null WHERE cid=" + courseid + ";";
                                        executeSQL(updateChapterSql, function (ret, err) {

                                        });
                                    });
                                } else {
                                }
                            });
                        }
                    });
                }
            });
            setTimeout(function () {
                $('.download_progress').attr('style', 'width(0)');
                $('.download_icon').removeClass('downloaded');
            }, 1000);
        });
        //监听取消某一章节
        api.addEventListener({
            name: 'cancelledChapter'
        }, function (ret, err) {
            var chapterid = ret.value.chapterid;
            openDB(function (ret, err) {
                if (ret.status) {
                    var sql = "UPDATE t_study_chapter SET localpath = null WHERE chapter_id = '" + chapterid + "';";
                    executeSQL(sql, function (ret, err) {
                        if (ret.status) {
                            // alert(JSON.stringify(ret));
                        } else {
                            // alert(JSON.stringify(err));
                        }
                        // closeDB(function(ret, err){});
                    });
                }
            });
        });
        //下载进度监听
        api.addEventListener({
            name: 'downlaodNotifi'
        }, function (ret, err) {
            if (ret) {
                // alert('00000' + JSON.stringify( ret ) );
                var result = ret.value.ret;
                if (result) {
                    var chapterid = ret.value.chapterid;
                    var chapterID = '#' + chapterid;
                    // alert(chapterID);
                    var width = "width:" + result.percent + "%;";
                    $(chapterID).find('.download_progress').attr('style', width);
                    // $('.download_progress').attr('style', width);
                    if (result.state == 1) {
                        $(chapterID).find('.download_progress').attr('style', 'width(0)');
                        $(chapterID).find('.download_icon').addClass('downloaded');
                    }
                } else {
                    // alert(ret.value.err.msg);
                }
            } else {
                // alert( JSON.stringify( err ) );
            }
        });
        //监听全部下载完成(为了防止下载过程异常,进度条不变化的情况，在全部下载完成后，再统一设置一次状态)
        api.addEventListener({
            name: 'filesDownloadFinishNotifi'
        }, function (ret, err) {
//		alert('testData=='+JSON.stringify(ret));
            var download_course_id = ret.value.courseid;
            var download_course_size = ret.value.coursesize;
            if (chapterParam.courseid == download_course_id) {
                for (var i = 0; i < chapterArr.length; i++) {
                    var chapterid = chapterArr[i].id;
                    var chapterID = '#' + chapterid;
                    $(chapterID).find('.download_progress').attr('style', 'width(0)');
                    $(chapterID).find('.download_icon').addClass('downloaded');
                }
            }
        });
        //监听下载失败
        api.addEventListener({
            name: 'downlaodFailedNotifi'
        }, function (ret, err) {
            // alert('下载失败章节页');
            var chapterid = ret.value.chapterid;
//	 		var connectionType = api.connectionType+'';
//				alert(connectionType);
//			if(connectionType=='none'){
//		      api.toast({
//               msg:'网络异常，请检查网络',
//               duration: 2000,
//			    location: 'bottom'
//           });
//			}else{
            api.toast({
                msg: '下载失败，请查看网络或者重新下载',
                duration: 2000,
                location: 'bottom'
            });
//	 		}
            var chapterID = '#' + chapterid;
            $(chapterID).find('.download_progress').attr('style', 'width(0)');
            $(chapterID).find('.download_icon').removeClass('downloaded');
        });
        //监听学习完成通知
        api.addEventListener({
            name: 'courseStudiedFinishNotifi'
        }, function (ret, err) {
            // alert('章节' + JSON.stringify(ret));
            console.log(JSON.stringify(ret));
            var chapter = ret.value;
            var cid = chapter.cid;
            var vid = chapter.vid;
            var studyType = chapter.studyType;
            if (studyType == '0') {
                // var timenum = parseInt(chapter.etime);
                // var origtime = parseInt(chapter.ctime);
                // var status = '2';
                // alert('学习时间' + timenum + '\n' + '章节总时间' + origtime);
                if (chapter.status == 1) {
                    // status = '1';
                    // timenum = origtime;
                    // alert('999999'+vid);
                    var chapterID = '#' + vid;
                    var chapterFirestP = $(chapterID).children('p:first');
                    if (chapterFirestP.hasClass('video_studying') || chapterFirestP.hasClass('video_unstudy')) {
                        chapterFirestP.addClass('video_studied');
                    } else if (chapterFirestP.hasClass('doc_studying') || chapterFirestP.hasClass('doc_unstudy')) {
                        chapterFirestP.addClass('doc_studied');
                    } else if (chapterFirestP.hasClass('richtext_studying') || chapterFirestP.hasClass('richtext_unstudy')) {
                        chapterFirestP.addClass('richtext_studied');
                    } else if (chapterFirestP.hasClass('ziyuan_studying') || chapterFirestP.hasClass('ziyuan_unstudy')) {
                        chapterFirestP.addClass('ziyuan_studied');
                    } else if (chapterFirestP.hasClass('tests_untest')) {
                        chapterFirestP.addClass('tests_tested');
                    } else if (chapterFirestP.hasClass('taolun_normal')) {
                        chapterFirestP.addClass('taolun_highlight');
                    }
                }
            } else {
                status = '1';
                var chapterID = '#' + vid;
                var chapterFirestP = $(chapterID).children('p:first');
                if (chapterFirestP.hasClass('video_studying') || chapterFirestP.hasClass('video_unstudy')) {
                    chapterFirestP.addClass('video_studied');
                } else if (chapterFirestP.hasClass('doc_studying') || chapterFirestP.hasClass('doc_unstudy')) {
                    chapterFirestP.addClass('doc_studied');
                } else if (chapterFirestP.hasClass('richtext_studying') || chapterFirestP.hasClass('richtext_unstudy')) {
                    chapterFirestP.addClass('richtext_studied');
                } else if (chapterFirestP.hasClass('ziyuan_studying') || chapterFirestP.hasClass('ziyuan_unstudy')) {
                    chapterFirestP.addClass('ziyuan_studied');
                } else if (chapterFirestP.hasClass('tests_untest')) {
                    chapterFirestP.addClass('tests_tested');
                } else if (chapterFirestP.hasClass('taolun_normal')) {
                    chapterFirestP.addClass('taolun_highlight');
                }
            }
        });
    }

    //加载学习进度设置考试按钮
    function loadStudyScheduleSetupExamBtn() {
        var param = {
            uid: $api.getStorage('cfnetppuid'),
            jid: $api.getStorage('cfnetppjxid'),
            pid: api.pageParam.pid,
            cid: api.pageParam.courseid
        };
        // console.log(url_getMoocSchedule+'\n'+JSON.stringify(param));
        cfnetppPOST(url_getMoocSchedule, param, false, function (data, status) {
            if (status == 'success') {
                if (data.code == 200) {
                    var schedule = data.data;
                    if (schedule == '100%') {
                        setupExamBtn();//设置考试按钮
                    }
                }
            }
        });
    }

    function setupExamBtn() {
        var examid = api.pageParam.examid;
        var params = {
            exam_id: api.pageParam.examid,
            user_id: $api.getStorage('cfnetppuid'),
            cid: api.pageParam.courseid,
            pid: api.pageParam.pid
        };
        cfnetppPOST(url_getCourseExamType, params, false, function (data, status) {
            if (status == 'success') {
                if (data.code == 200) {
                    var is_score = data.data[0].is_score;
                    score_id = data.data[0].score_id;
                    score = data.data[0].score;
                    if (examid != undefined && examid != null && examid != '' && examid != '0' && examid != "") {
                        if (is_score == 4 || is_score == 6) {//开始考试
                            $("#study_btn").text("开始考试");
                            open_type = 0;
                            $(".blank-div").show();
                            $(".footer-view").show();
                        } else if (is_score == 1 || is_score == 2 || is_score == 3 || is_score == 5) {//查看考试结果
                            $("#study_btn").text("查看考试结果");
                            open_type = 1;
                            $(".blank-div").show();
                            $(".footer-view").show();
                        } else {
                            $(".blank-div").hide();
                            $(".footer-view").hide();
                        }
                    } else {
                        $(".blank-div").hide();
                        $(".footer-view").hide();
                    }
                } else if (data.code == 108) {
                    if (examid != undefined && examid != null && examid != '' && examid != "0" && examid != 0) {
                        $(".blank-div").show();
                        $(".footer-view").show();
                        $("#study_btn").text("开始考试");
                        open_type = 0;
                    } else {
                        $(".blank-div").hide();
                        $(".footer-view").hide();
                        open_type = 1;
                    }

                } else {
                    $(".blank-div").hide();
                    $(".footer-view").hide();
                }
            } else {
                $(".blank-div").hide();
                $(".footer-view").hide();
            }
        });
    }


    function sendBuriedNotifi(busionessid, pagetype, pagetypeid, vid) {
        var userinfo = {
            uid: $api.getStorage('cfnetppuid'),
            jid: $api.getStorage('cfnetppjxid'),
            uname: $api.getStorage('cfnetppusername'),
            cid: api.pageParam.courseid,
            pid: api.pageParam.pid,

        }
        var data = {
            busionessid: busionessid,
            pagetype: pagetype,
            pagetypeid: pagetypeid,
            sourceurl: api.pageParam.sourceurl,
            sourcetitle: api.pageParam.sourcetitle,
            accesspageurl: window.location.pathname,
            accesspagetitle: document.title,
            userinfo: userinfo
        }
        if (vid != "") {
            userinfo["vid"] = vid;
            data["accesspagetitle"] = "Mocc视频章节";
        }
        SendBehaviorTrajectoriesNotifi_Study(data);
    }

    function onExamClick() {

        api.openWin({
            useWKWebView: true,
            name: 'Student_CourseTestExam',
            url: 'Student_CourseTestExam.html',
            pageParam: {
                appkey: 148745497845,
                match_type: 3,
                exam_id: api.pageParam.examid,
                plan_id: api.pageParam.pid,
                course_id: api.pageParam.courseid,
                score_id: score_id,
                open_type: open_type,
                score: score,
                type: 0,//mocc课程
                sourceurl: window.location.pathname,
                sourcetitle: document.title,
                openType: api.pageParam.openType
            }
        });

//		sendBuriedNotifi('1', 'mooc章节列表', '018——1');
    }

    //下拉刷新
    function setRefreshHeader() {
        api.setRefreshHeaderInfo({
            visible: true,
            bgColor: '#ccc',
            textColor: '#fff',
            textDown: '下拉刷新...',
            textUp: '松开刷新...',
            showTime: true
        }, function (ret, err) {
            loadData();
        });
    }

    function loadData() {
        requestionData();
    }

    function requestionData() {
        api.showProgress({
            style: 'default',
            animationType: 'fade',
            title: '加载中...',
            modal: false
        });
        // alert(study_course_chapter + '\n' + chapterParam.courseid);
        var params = {
            cid: chapterParam.courseid,
//  		cstatus:chapterParam.cstatus,
//  		source:chapterParam.source,
            uid: uid,
            jid: jid,
            pid: chapterParam.pid
        };
        // console.log('url:'+url_freeChapterUrl+'\n'+JSON.stringify(params));
//	 var freeChapterUrl = "http://base.cfnet.org.cn/course/course_normal/getChapterList";
        cfnetppPOST(url_freeChapterUrlNew, params, false, function (data, status) {
            api.refreshHeaderLoadDone();
            api.hideProgress();
            if (status == 'success') {
                if (data.status) {
                    //存储最后刷新的一次时间戳
                    saveTimeStamp();
                    var chapterlist = data.data;
                    if (chapterlist == null || chapterlist == undefined || chapterlist == '') {
                        $('.noDataDiv').show();
                        $('.noInternetDiv').hide();
                    } else {
                        $('.noDataDiv').hide();
                        $('.noInternetDiv').hide();
                        setupPage(chapterlist, 1);
                    }
                } else {
                    openDB(function (ret, err) {
                        if (ret.status) {
                            var sql = "SELECT * FROM t_study_chapter WHERE cid = '" + chapterParam.courseid + "';";
                            selectSQL(sql, function (ret, err) {
                                if (ret.status && ret.data != undefined && ret.data != null && ret.data != '') {
                                    var chapterlist = ret.data;
                                    setupPage(chapterlist, 0);//loadType = 0 时不需要存入数据库
                                } else {
                                    api.toast({
                                        msg: '暂无数据',
                                        duration: 2000,
                                        location: 'bottom'
                                    });
                                    $('.noDataDiv').show();
                                    $('.noInternetDiv').hide();
                                }
                            });
                        }
                    });
                }
            } else {
                openDB(function (ret, err) {
                    if (ret.status) {
                        var sql = "SELECT * FROM t_study_chapter WHERE cid = '" + chapterParam.courseid + "';";
                        selectSQL(sql, function (ret, err) {
                            if (ret.status && ret.data != undefined && ret.data != null && ret.data != '') {
                                var chapterlist = ret.data;
                                setupPage(chapterlist, 0);//loadType = 0 时不需要存入数据库
                            } else {
                                api.toast({
                                    msg: '数据加载失败',
                                    duration: 2000,
                                    location: 'bottom'
                                });
                                $('.noDataDiv').hide();
                                $('.noInternetDiv').show();
                            }
                        });
                    }
                });
            }
        });
    }

    function saveTimeStamp() {
        var timestamp = new Date().getTime();
        var timeStampStr = timestamp.toString();
        $api.setStorage('chapterLastTimeStamp', timeStampStr);
        $api.setStorage('chapterLastCourseId', api.pageParam.courseid);
        $api.setStorage('chapterLastPlanId', api.pageParam.pid);
    }

    function setupPage(chapterlist, loadType) {
        var chapterClassIndex = 0;
        var chapterSectionIndex = 0;
        var chapterHtmlString = '';
        var pid = api.pageParam.pid;
        for (var i = 0; i < chapterlist.length; i++) {
            var chapter = chapterlist[i];
            var chapter_id;
            if (loadType == 0) {
                chapter_id = chapter.chapter_id;
            } else {
                chapter_id = chapter.id;
            }
            // alert(JSON.stringify(chapter));
            var rid = chapter.rid;
            var sourceUrl = chapter.sourceUrl;
            var chapterParam = {
                chapter_id: chapter_id,
                cid: chapter.cid,
                handout: chapter.handout,
                rid: rid,
                cdetailtitle: chapter.cdetailtitle,
                ctime: chapter.ctime,
                etime: chapter.etime,
                other: chapter.other,
                exam_id: chapter.exam,
                // pid : chapter.pid,
                pid: pid,
                path: chapter.path,
                content_type: chapter.content_type,
                bewrite: chapter.bewrite,
                is_dis_bewrite: chapter.is_dis_bewrite,
                weights: chapter.weights,
                isclose: chapter.isclose,
                mtime: chapter.mtime,
                paragh: chapter.paragh,
                hdresourceType: chapter.hdresourceType,
                is_encr: chapter.is_encr,
                baidu_cloud_id: chapter.baidu_cloud_id,
                baidu_cloud_id_url: chapter.baidu_cloud_id_url,
                sourceUrl: sourceUrl
            };
            // alert('章节数据:'+JSON.stringify(chapterParam));
            var cid = chapter.cid;
            var uniquekey = cid + chapter_id + uid;
            var chapterRecordParam = {
                uid: uid,
                jid: jid,
                pid: pid,
                vid: chapter_id,
                cid: cid,
                crid: crid,
                types: '1',//章节
                origtime: chapter.ctime,
                status: chapter.status,
                timenum: chapter.timenum,
                uniquekey: uniquekey
            }
            // alert('学习记录数据:'+JSON.stringify(chapterRecordParam));
            if (loadType == 1) {//刚请求下的数据

                cachingChapterData(chapterParam);
                cachingChapterRecord(chapterRecordParam);
            }
            if (chapter.content_type == 'tag') {

                chapterClassIndex++;
            } else if (chapter.content_type == 'part') {

                chapterSectionIndex++;
            } else if (chapter.content_type == 'catetag') {
                // chapterSectionIndex++;
            }
            var chapterString = htmlStringBuilder(chapterParam, chapterClassIndex, chapterSectionIndex);
            chapterHtmlString = chapterHtmlString + chapterString;
        }
        $('#chapter_content').html(chapterHtmlString);
        addSlideToggleClick();
        setupDownloadedStatus();
        setupStudyRecordStatus(chapterlist, loadType);
    }

    //缓存章节数据
    function cachingChapterData(chapterParam) {
        insertData('t_study_chapter', chapterParam, function (ret, err) {
            if (ret.status) {

            } else {
                // api.alert({
                //     msg: JSON.stringify(err)
                // });
            }
        });
    }

    //缓存课程记录数据
    function cachingChapterRecord(chapterRecordParam) {
        insertData('t_study_course_record', chapterRecordParam, function (ret, err) {
            if (ret.status) {
                // api.alert({
                //     msg: JSON.stringify(ret)
                // });
            } else {
                // api.alert({
                //     msg: JSON.stringify(err)
                // });
                var sql = "SELECT timenum FROM t_study_course_record WHERE vid=" + chapterRecordParam.vid + " AND uid=" + uid + ";";
                selectSQL(sql, function (ret, err) {
                    if (ret.status) {
                        // alert(JSON.stringify(ret)+"==="+ret.data.length);
                        if (ret.data.length > 0) {
                            var etime = parseInt(ret.data[0].timenum) * 60;//本地的
                            var timenum = parseInt(chapterRecordParam.timenum);//网络的学习时间
                            // alert(etime+'\n'+timenum);
                            if (timenum > etime) {
                                // var updateSql = "UPDATE t_study_course_record SET timenum=" + chapterRecordParam.timenum + ",status=" + chapterRecordParam.status + " , crid=" + crid + " WHERE vid=" + chapterRecordParam.vid + " AND uid=" + uid + ";";
                                var updateSql = "UPDATE t_study_course_record SET timenum=" + chapterRecordParam.timenum + ",status=" + chapterRecordParam.status + " WHERE vid=" + chapterRecordParam.vid + " AND uid=" + uid + ";";
                                executeSQL(updateSql, function (ret, err) {
                                    if (ret.status) {
                                        // alert(JSON.stringify(ret));
                                    } else {
                                        // alert(JSON.stringify(err));
                                    }
                                });
                            }
                        }
                    }
                });
            }
        });
    }

    function setupDownloadedStatus() {
        var sql = "SELECT chapter_id FROM t_study_chapter WHERE cid = '" + chapterParam.courseid + "' AND localpath IS NOT NULL;";
        selectSQL(sql, function (ret, err) {
            if (ret.status && ret.data != null && ret.data != '' && ret.data != undefined) {
                var chapters = ret.data;
                for (var i = 0; i < chapters.length; i++) {
                    var chapterid = chapters[i].chapter_id;
                    var chapterID = '#' + chapterid;
                    $(chapterID).find('.download_icon').addClass('downloaded');
                }
            } else {
            }
        });
    }

    function setupStudyRecordStatus(chapterlist, loadType) {
        // console.log(JSON.stringify(chapterlist));
        for (var i = 0; i < chapterlist.length; i++) {
            var vid;
            var chapterData = chapterlist[i];
            if (loadType == 0) {
                vid = chapterData.chapter_id;
            } else {
                vid = chapterData.id;
            }
            setupChapterStatus(vid, chapterData.status);
        }
    }

    function setupChapterStatus(vid, netStatus) {
        selectContentType(vid, function (current_vid, chaptertag) {
            selectStudyStatus(current_vid, chaptertag, function (chapter_id, status, chapter_content_type) {
                if (netStatus == 1) {
                    status = 1;
                }
                var chapterID = '#' + chapter_id;
                if (status == 1) {
                    if (chapter_content_type == 'video') {
                        $(chapterID).children('p:first').addClass('video_studied');
                    } else if (chapter_content_type == 'doc') {
                        $(chapterID).children('p:first').addClass('doc_studied');
                    } else if (chapter_content_type == 'richtext') {
                        $(chapterID).children('p:first').addClass('richtext_studied');
                    } else if (chapter_content_type == 'hdresource') {
                        $(chapterID).children('p:first').addClass('ziyuan_studied');
                    } else if (chapter_content_type == 'tests') {
                        $(chapterID).children('p:first').addClass('tests_tested');
                    } else if (chapter_content_type == 'discuss') {
                        $(chapterID).children('p:first').addClass('taolun_highlight');
                    }
                } else if (status == 2) {
                    if (chapter_content_type == 'video') {
                        $(chapterID).children('p:first').addClass('video_studying');
                    } else if (chapter_content_type == 'doc') {
                        $(chapterID).children('p:first').addClass('doc_studying');
                    } else if (chapter_content_type == 'richtext') {
                        $(chapterID).children('p:first').addClass('richtext_studying');
                    } else if (chapter_content_type == 'hdresource') {
                        $(chapterID).children('p:first').addClass('ziyuan_studying');
                    } else if (chapter_content_type == 'tests') {
                        $(chapterID).children('p:first').addClass('tests_tested');
                    } else if (chapter_content_type == 'discuss') {
                        $(chapterID).children('p:first').addClass('taolun_highlight');
                    }
                }
            });
        });
    }

    function selectContentType(vid, callBack) {
        var sql = "SELECT content_type FROM t_study_chapter WHERE chapter_id='" + vid + "';";
        // alert('selectContentType~' + sql);
        selectSQL(sql, function (ret, err) {
            // alert('selectContentType~'+JSON.stringify(ret)+'\n'+JSON.stringify(err));
            if (ret.status && ret.data != undefined && ret.data != null && ret.data != '') {
                var chaptertag = ret.data[0].content_type;
                callBack(vid, chaptertag);
            }
        });
    }

    function selectStudyStatus(vid, chaptertag, callBack) {
        var sql = "SELECT status FROM t_study_course_record WHERE vid=" + vid + " and uid = " + uid + ";";
        // alert('selectStudyStatus~'+sql);
        selectSQL(sql, function (ret, err) {
            // alert('selectStudyStatus~'+vid+JSON.stringify(ret)+'\n'+JSON.stringify(err));
            if (ret.status && ret.data != undefined && ret.data != null && ret.data != '') {
                var status = ret.data[0].status;
                callBack(vid, status, chaptertag);
            }
        });
    }

    function startStudying(vid) {
        // openDB(function(ret, err){
        // if (ret.status) {
        var chapterID = '#' + vid;
        var chapterFirestP = $(chapterID).children('p:first');
        if (chapterFirestP.hasClass('video_studied') || chapterFirestP.hasClass('doc_studied') || chapterFirestP.hasClass('richtext_studied') || chapterFirestP.hasClass('ziyuan_studied') || chapterFirestP.hasClass('tests_tested') || chapterFirestP.hasClass('taolun_highlight')) {
            // alert('已学完');
            return;
        }
        // var sql = "UPDATE t_study_course_record SET status = 2, crid=" + crid + " WHERE vid=" + vid + " and uid = " + uid + ";";
        var sql = "UPDATE t_study_course_record SET status = 2" + " WHERE vid=" + vid + " and uid = " + uid + ";";
        executeSQL(sql, function (ret, err) {
            if (ret.status) {
                // alert('更新语句' + JSON.stringify(sql));
                // alert('更新结果' + JSON.stringify(ret));
            } else {
                // alert('更新错误' + JSON.stringify(err));
            }
        });
        // }
        // });
    }

    function addSlideToggleClick() {
// 	  SendBehaviorTrajectoriesNotifi('0', 'mooc章节列表', '008');
        //点击章收放显示
        $('.weui_panel_hd').click(function (event) {
            var chapterClass = "." + $(this).attr('value');
            $(chapterClass).slideToggle(0);
        });
        $('.chapter_content_cell').click(function (event) {
            $('.chapter_content_cell').removeClass('base_style_text_color');
            $(this).addClass('base_style_text_color');
        });
        $('.works_cell').click(function (event) {
            sendBuriedNotifi('1', 'mooc章节列表', '008', '');
            var vid = $(this).attr('id');
            var cid = $(this).attr('cid');
            var pid = $(this).attr('pid');
            var title = $(this).attr('title');
            var bewrite = $(this).attr('bewrite');
            var is_dis_bewrite = $(this).attr('is_dis_bewrite');
            api.openWin({
                useWKWebView: true,
                name: 'new_student_study_freecourse_homeworks',
                url: '../Study/new_student_study_freecourse_homeworks.html',
                pageParam: {
                    pid: pid,
                    chapterid: vid,
                    cdetailtitle: title,
                    bewrite: bewrite,
                    is_dis_bewrite: is_dis_bewrite,
                    cid: cid,
                    sourceurl: window.location.pathname,
                    sourcetitle: document.title,
                }
            });
        });
        //点击文档课程事件
        $('.doc_cell').click(function () {
            sendBuriedNotifi('1', 'mooc章节列表', '008', '');
            if (isSelected == 0) {//如果未选当前课程则不能学习
                api.toast({
                    msg: '您还没有添加该课程',
                    duration: 3000,
                    location: 'bottom'
                });
                return;
            }
            if (isFreeCourse == false) {//付费课程未购买
                api.toast({
                    msg: '该课程为付费课程，购买后才可学习',
                    duration: 3000,
                    location: 'bottom'
                });
                return;
            }
            var isclose = $(this).attr('isclose');
            if (isclose == 0) {
                api.toast({
                    msg: '该课时尚未发布，暂不能学习',
                    duration: 3000,
                    location: 'bottom'
                });
                return;
            }
            var vid = $(this).attr('id');
            var cid = $(this).attr('cid');
            var rid = $(this).attr('rid');
            var pid = $(this).attr('pid');
            var ctime = $(this).attr('ctime');
            var title = $(this).attr('title');
            var is_encr = $(this).attr('is_encr');
            var baidu_cloud_id = $(this).attr('baidu_cloud_id');
            var baidu_cloud_id_url = $(this).attr('baidu_cloud_id_url');
            currentParam = {
                cid: cid,
                pid: pid,
                vid: vid,
            };
            // if (vid != allChapters[0].id && checks == 1) {
            // 	for (var i = 0; i < allChapters.length; i++) {
            // 		if (vid == allChapters[i].id) {
            // 			prevVid = allChapters[i - 1].id;
            // 			var chapterID = '#' + prevVid;
            // 			var chapterPrevDiv = $(chapterID);
            // 			if (chapterPrevDiv.hasClass('chapter_content_cell')) {
            // 				var chapterPrevDivP = chapterPrevDiv.children("p.chapter_cell_icon");
            // 				if (!chapterPrevDivP.hasClass('doc_studied') && !chapterPrevDivP.hasClass('video_studied') && !chapterPrevDivP.hasClass('richtext_studied')) {
            // 					api.toast({
            // 					    msg: '您还有未学完的课程，请学完后再开始学习下一章节',
            // 					    duration: 5000,
            // 					    location: 'bottom'
            // 					});
            // 					return;
            // 				}
            // 			}
            // 		}
            // 	}
            // }


            openDB(function (ret, err) {
                // if (ret.status) {
                var sql = "SELECT timenum,status FROM t_study_course_record WHERE vid = " + vid + " and uid = " + uid + ";";
                selectSQL(sql, function (ret, err) {
                    // if (ret.status && ret.data != null && ret.data != undefined && ret.data != '') {
                    var etime;
                    if (ret.status && ret.data != null && ret.data != undefined && ret.data != '') {
                        etime = ret.data[0].timenum;
                    }
                    if (etime == null || etime == undefined || etime == '') {
                        etime = '0';
                    }
                    var status = ret.data[0].status;
                    var localpathSql = "SELECT localpath FROM t_study_chapter WHERE chapter_id = '" + vid + "';";
                    var openLocalDoc = false;//是否打开本地文档
                    selectSQL(localpathSql, function (ret, err) {
                        if (ret.status && ret.data != null && ret.data != undefined && ret.data != '') {
                            var localpath = ret.data[0].localpath;
                            if (localpath != null && localpath != undefined && localpath != '') {
                                rid = localpath;
                                openLocalDoc = true;
                            }
                        }
                        clearInterval(studyTimeRecord);
                        if (openLocalDoc == true) {
                            if (parseInt(etime) < parseInt(ctime) || status != 1) {
                                // var updateDocSql = "UPDATE t_study_course_record SET submitstatus=2,status=1,timenum=" + ctime + " , crid=" + crid + " WHERE vid=" + vid + " and uid = " + uid + ";";
                                var updateDocSql = "UPDATE t_study_course_record SET submitstatus=2,status=1,timenum=" + ctime + " WHERE vid=" + vid + " and uid = " + uid + ";";
                                executeSQL(updateDocSql, function (ret, err) {
                                    if (ret.status) {
                                        var connectionType = api.connectionType + '';

                                        if (connectionType != none) {
                                            var stParam = {
                                                title: title,
                                                doc: rid,
                                                vid: vid,
                                                cid: cid,
                                                pid: pid,
                                                ctime: ctime,
                                                etime: etime,
                                                cstatus: chapterParam.cstatus,
                                                status: status,
                                                crid: crid,
                                                examid: chapterParam.examid,
                                                allChapters: allChapters

                                            };
                                            // 每分钟纪录一次时间
                                            // studyTimeRecord=setInterval(function(){
                                            //         // insertDataFun('2');
                                            //     requestTimeControlFun(stParam);
                                            // },60000);
                                        } else {
                                            // 												setTimeout(function(){
                                            // //												alert("nihao1");
                                            // 													setupCourseStudyStatus(cid);
                                            // //													$('#' + vid).children('p:first').addClass('doc_studied');

                                            // 												}, 60000)
                                        }
                                    }
                                });
                            }
                            var docReader = api.require('docReader');
                            docReader.open({
                                path: rid
                            });
                        } else {
                            var connectionType = api.connectionType + '';
                            //							alert("文档"+connectionType);
                            if (connectionType == 'none') {
                                api.toast({
                                    msg: '网络异常，请检查网络',
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            } else {
                                var studyParam = {
                                    title: title,
                                    doc: rid,
                                    vid: vid,
                                    cid: cid,
                                    pid: pid,
                                    ctime: ctime,
                                    etime: etime,
                                    cstatus: chapterParam.cstatus,
                                    status: status,
                                    crid: crid,
                                    examid: chapterParam.examid,
                                    allChapters: allChapters,
                                    sourceurl: window.location.pathname,
                                    sourcetitle: document.title,
                                };
                                study_courseTimeRecordFun(studyParam, 'NONE', 'false');
                                if (is_encr == 1) {//加密
                                    api.openWin({
                                        useWKWebView: true,
                                        name: 'StudyWebBaiDuDoc_win',
                                        url: 'StudyWebBaiDuDoc_win.html',
                                        pageParam: {
                                            docId: baidu_cloud_id,
                                            title: title,
                                            studyParam: studyParam
                                        }
                                    });
                                    // var CFBaiDuDoc = api.require('CFBaiDuDoc');
                                    //       CFBaiDuDoc.startDocActivityForResult({
                                    //           docId:baidu_cloud_id,//文档ID
                                    //           title:title,//文档标题
                                    //           token:"TOKEN",//文档token，离线加载时不验证。
                                    //           docType:"doc" //文档类型，可取值为ppt 和 doc
                                    //       },function(){
                                    //       	study_courseTimeRecordFun(studyParam, 'NONE', 'true');
                                    //       });
                                } else {
                                    api.openWin({
                                        useWKWebView: true,
                                        name: 'studyCourseDoc',
                                        url: 'studyCourseDoc.html',
                                        bounces: false,
                                        pageParam: studyParam,
                                        bgColor: '#fff'
                                    });
                                }
                            }
                        }
                    });
                    // }
                });

                // }
            });
            //设置正在学习状态
            var chapterID = '#' + vid;
            $(chapterID).children('p:first').addClass('doc_studying');
            startStudying(vid);
        });
        $('.richtext_cell').click(function () {//图文
            sendBuriedNotifi('1', 'mooc章节列表', '008', '');
            if (isSelected == 0) {//如果未选当前课程则不能学习
                api.toast({
                    msg: '您还没有添加该课程',
                    duration: 3000,
                    location: 'bottom'
                });
                return;
            }
            if (isFreeCourse == false) {//付费课程未购买
                api.toast({
                    msg: '该课程为付费课程，购买后才可学习',
                    duration: 3000,
                    location: 'bottom'
                });
                return;
            }
            var isclose = $(this).attr('isclose');
            if (isclose == 0) {
                api.toast({
                    msg: '该课时尚未发布，暂不能学习',
                    duration: 3000,
                    location: 'bottom'
                });
                return;
            }
            var vid = $(this).attr('id');
            var cid = $(this).attr('cid');
            var other = $(this).attr('other');
            var pid = $(this).attr('pid');
            var ctime = $(this).attr('ctime');
            var title = $(this).attr('title');
            var bewrite = $(this).attr('bewrite');
            currentParam = {
                cid: cid,
                pid: pid,
                vid: vid
            };
            // if (vid != allChapters[0].id && checks == 1) {
            // 	for (var i = 0; i < allChapters.length; i++) {
            // 		if (vid == allChapters[i].id) {
            // 			prevVid = allChapters[i - 1].id;
            // 			var chapterID = '#' + prevVid;
            // 			var chapterPrevDiv = $(chapterID);
            // 			if (chapterPrevDiv.hasClass('chapter_content_cell')) {
            // 				var chapterPrevDivP = chapterPrevDiv.children("p.chapter_cell_icon");
            // 				if (!chapterPrevDivP.hasClass('doc_studied') && !chapterPrevDivP.hasClass('video_studied') && !chapterPrevDivP.hasClass('richtext_studied')) {
            // 					api.toast({
            // 					    msg: '您还有未学完的课程，请学完后再开始学习下一章节',
            // 					    duration: 5000,
            // 					    location: 'bottom'
            // 					});
            // 					return;
            // 				}
            // 			}
            // 		}
            // 	}
            // }

            openDB(function (ret, err) {
                // if (ret.status) {
                var sql = "SELECT timenum,status FROM t_study_course_record WHERE vid = " + vid + " and uid = " + uid + ";";
                selectSQL(sql, function (ret, err) {
//							 alert(JSON.stringify(ret));
                    // if (ret.status && ret.data != null && ret.data != undefined && ret.data != '') {
                    var etime = '0';
                    var timenum = ret.data.timenum;
                    if (ret.status && timenum != null && timenum != undefined && timenum != '') {

                        etime = ret.data[0].timenum;
                    }
                    if (etime == null || etime == undefined || etime == '') {

                        etime = '0';
                    }
                    var status = ret.data[0].status;
                    var studyParam = {
                        title: title,
                        other: other,
                        vid: vid,
                        cid: cid,
                        pid: pid,
                        ctime: ctime,
                        etime: etime,
                        cstatus: chapterParam.cstatus,
                        status: status,
                        crid: crid,
                        examid: chapterParam.examid,
                        allChapters: allChapters,
                        richtextType: 2,
                        bewrite: bewrite,
                        cdetailtitle: title,
                        userType: 'student',
                        sourceurl: window.location.pathname,
                        sourcetitle: document.title,
                    };
                    // alert('章节参数' + JSON.stringify(studyParam));
                    study_courseTimeRecordFun(studyParam, 'NONE', 'false');
                    api.openWin({
                        useWKWebView: true,
                        name: 'studyCourseRichtextNew',
                        url: 'studyCourseRichtextNew.html',
                        bounces: false,
                        pageParam: studyParam,
                        bgColor: '#fff'
                    });
                    // }
                });
                // }
            });
            //设置正在学习状态
            var chapterID = '#' + vid;
            $(chapterID).children('p:first').addClass('richtext_studying');
            startStudying(vid);
        });
        //点击互动资源
        $('.ziyuan_cell').click(function () {
            sendBuriedNotifi('1', 'mooc章节列表', '008', '');
            var isclose = $(this).attr('isclose');
            if (isclose == 0) {
                api.toast({
                    msg: '该课时尚未发布，暂不能学习',
                    duration: 3000,
                    location: 'bottom'
                });
                return;
            }
            var vid = $(this).attr('id');
            var cid = $(this).attr('cid');
            var other = $(this).attr('other');
            var pid = $(this).attr('pid');
            var ctime = $(this).attr('ctime');
            var title = $(this).attr('title');
            var bewrite = $(this).attr('bewrite');
            var hdresourceType = $(this).attr('hdresourceType');
            currentParam = {
                cid: cid,
                pid: pid,
                vid: vid
            };
            openDB(function (ret, err) {
                // if (ret.status) {
                var sql = "SELECT timenum,status FROM t_study_course_record WHERE vid = " + vid + " and uid = " + uid + ";";
                selectSQL(sql, function (ret, err) {
//							 alert(JSON.stringify(ret));
                    // if (ret.status && ret.data != null && ret.data != undefined && ret.data != '') {
                    var etime = '0';
                    var timenum = ret.data.timenum;
                    if (ret.status && timenum != null && timenum != undefined && timenum != '') {

                        etime = ret.data[0].timenum;
                    }
                    if (etime == null || etime == undefined || etime == '') {

                        etime = '0';
                    }
                    var status = ret.data[0].status;
                    var studyParam = {
                        title: title,
                        other: other,
                        vid: vid,
                        cid: cid,
                        pid: pid,
                        ctime: ctime,
                        etime: etime,
                        cstatus: chapterParam.cstatus,
                        status: status,
                        crid: crid,
                        examid: chapterParam.examid,
                        allChapters: allChapters,
                        bewrite: bewrite,
                        hdresourceType: hdresourceType,
                        id: vid,
                        sourceurl: window.location.pathname,
                        sourcetitle: document.title,
                        ismocc: 1,
                    };
                    study_courseTimeRecordFun(studyParam, 'NONE', 'false');
//						     alert('章节参数' + JSON.stringify(studyParam));
                    api.openWin({
                        useWKWebView: true,
                        name: 'student_study_freecourse_interactiveNew',
                        url: 'student_study_freecourse_interactiveNew.html',
                        bounces: false,
                        pageParam: studyParam,
                        bgColor: '#fff'
                    });
                    // }
                });
                // }
            });
            //设置正在学习状态
            var chapterID = '#' + vid;
            $(chapterID).children('p:first').addClass('ziyuan_studying');
            startStudying(vid);
        });
        //点击测验
        $('.tests_cell').click(function () {
            sendBuriedNotifi('1', 'mooc章节列表', '008', '');
            if (isSelected == 0) {//如果未选当前课程则不能学习
                api.toast({
                    msg: '您还没有添加该课程',
                    duration: 3000,
                    location: 'bottom'
                });
                return;
            }
            if (isFreeCourse == false) {//付费课程未购买
                api.toast({
                    msg: '该课程为付费课程，购买后才可学习',
                    duration: 3000,
                    location: 'bottom'
                });
                return;
            }
            var isclose = $(this).attr('isclose');
            if (isclose == 0) {
                api.toast({
                    msg: '该课时尚未发布，暂不能学习',
                    duration: 3000,
                    location: 'bottom'
                });
                return;
            }
            var vid = $(this).attr('id');
            var cid = $(this).attr('cid');
            var rid = $(this).attr('rid');
            var pid = $(this).attr('pid');
            var title = $(this).attr('title');
            var bewrite = $(this).attr('bewrite');
            var is_dis_bewrite = $(this).attr('is_dis_bewrite');

            var recordParam = {
                vid: vid,
                cid: cid,
                pid: pid
            };
            study_courseTimeRecordFun(recordParam, 'NONE', 'false');
            currentParam = recordParam;
            // alert("bewrite:"+bewrite+"\nis_dis:"+is_dis_bewrite);
//			var selectTestSql = "SELECT isanswer FROM t_study_test_record WHERE chapterid=" + vid + " AND uid=" + uid + ";";
//  			selectSQL(selectTestSql, function(ret, err){
//  				if (ret.data.constructor == Array && ret.data.length > 0) {
//  					var winName = 'studyTestDetail';
//  					var winUrl = 'studyTestDetail.html';
//  					if (ret.data[0].isanswer == 1) {
//  						winName = 'studyTestFinishDetail';
//  						winUrl = 'studyTestFinishDetail.html';
//							//防止通知发送失败，不能刷新测试显示状态
//							$('#' + vid).children('p:first').addClass('tests_tested');
//  					}
            var pageParam = {
                id: vid,
                pid: pid,
                cid: cid,
                title: title,
                rid: rid,
                type: 0,
                sourceurl: window.location.pathname,
                sourcetitle: document.title,
                openType: api.pageParam.openType
            };
            $api.setStorage('Test_is_dis_bewrite', is_dis_bewrite);
            $api.setStorage('Test_bewrite', bewrite);
            $api.setStorage('Test_title', title);
            api.openWin({
                useWKWebView: true,
                name: 'New_student_study_freecourse_TestExam',
                url: 'New_student_study_freecourse_TestExam.html',
                bounces: false,
                pageParam: pageParam,
                slidBackEnabled: false
            });
//  				}
//  			});
        });
        //点击讨论
        $('.discuss_cell').click(function () {
            sendBuriedNotifi('1', 'mooc章节列表', '008', '');
            if (isSelected == 0) {//如果未选当前课程则不能学习
                api.toast({
                    msg: '您还没有添加该课程',
                    duration: 3000,
                    location: 'bottom'
                });
                return;
            }
            if (isFreeCourse == false) {//付费课程未购买
                api.toast({
                    msg: '该课程为付费课程，购买后才可学习',
                    duration: 3000,
                    location: 'bottom'
                });
                return;
            }
            var isclose = $(this).attr('isclose');
            if (isclose == 0) {
                api.toast({
                    msg: '该课时尚未发布，暂不能学习',
                    duration: 3000,
                    location: 'bottom'
                });
                return;
            }
            var vid = $(this).attr('id');
            var cid = $(this).attr('cid');
            var rid = $(this).attr('rid');
            var pid = $(this).attr('pid');
            var title = $(this).attr('title');
            var bewrite = $(this).attr('bewrite');

            var recordParam = {
                vid: vid,
                cid: cid,
                pid: pid
            };
            study_courseTimeRecordFun(recordParam, 'NONE', 'false');
            currentParam = recordParam;
//			var selectTestSql = "SELECT isanswer FROM t_study_test_record WHERE chapterid=" + vid + " AND uid=" + uid + ";";
//  			selectSQL(selectTestSql, function(ret, err){
//  				if (ret.data.constructor == Array && ret.data.length > 0) {
//  					var winName = 'studyTestDetail';
//  					var winUrl = 'studyTestDetail.html';
//  					if (ret.data[0].isanswer == 1) {
//  						winName = 'studyTestFinishDetail';
//  						winUrl = 'studyTestFinishDetail.html';
//							//防止通知发送失败，不能刷新测试显示状态
//							$('#' + vid).children('p:first').addClass('tests_tested');
//  					}
            var pageParam = {
                chapterid: vid,
                pid: pid,
                cid: cid,
                title: title,
                rid: rid,
                bewrite: bewrite,
                type: 0,
                sourceurl: window.location.pathname,
                sourcetitle: document.title,
            };
            api.openWin({
                useWKWebView: true,
                name: 'Student_Mocc_DiscussChapterWindow',
                url: 'Student_Mocc_DiscussChapterWindow.html',
                bounces: false,
                pageParam: pageParam
            });
//  				}
//  			});
        });
        //点击视频课程事件
        $('.video_cell').click(function () {
            getCourseUrl($(this).attr('id'), this);
        });


    }

    function toTempVideo(cid, vid) {
        api.openWin({
            useWKWebView: true,
            name: 'TempPlayVideo',
            url: '../Teacher_Teach/TempPlayVideo.html',
            pageParam: {
                cid: cid,
                vid: vid
            }
        });
    }

    function toBaiDuPlayer(is_encr, baidu_cloud_id_url, path, title, videoParam) {
        if (api.connectionType == 'none') {//暂无网络
            api.toast({
                msg: '暂无网络，无法访问该资源'
            });
            return;
        }
        if (path != null && path != '' && path.indexOf("http://") < 0) {
            path = 'http://bos.cfnet.org.cn' + path;
        }
        var videoPath = path;
        if (is_encr == 2) {//转码
            videoPath = baidu_cloud_id_url;
            sendBuriedNotifi('0', 'mooc视频', '009', videoParam.vid);
            api.openWin({
                useWKWebView: true,
                name: 'BaiDuWebVideo',
                url: 'BaiDuWebVideo.html',
                pageParam: {
                    title: title,
                    url: videoPath,
                    token: '',
                    videoParam: videoParam
                },
                slidBackEnabled: false
            });
        } else {

            sendBuriedNotifi('0', 'mooc视频', '009', videoParam.vid);
            api.openWin({
                useWKWebView: true,
                name: 'BaiDuWebVideo',
                url: 'BaiDuWebVideo.html',
                pageParam: {
                    title: title,
                    url: videoPath,
                    token: '',
                    videoParam: videoParam
                },
                slidBackEnabled: false
            });
        }

    }

    function toPlayVideo(path, title, playParam) {
        if (path != null && path != '' && path.indexOf("http://") < 0) {
            path = 'http://bos.cfnet.org.cn' + path;
        }
        // console.log(path);
        var h = 240;
        api.openFrame({
            name: 'HTML5VideoPlayer',
            url: '../Teacher_Teach/HTML5VideoPlayer.html',
            rect: {
                x: 0,
                y: 0,
                w: 'auto',
                h: h
            },
            pageParam: {
                isStuMooc: true,
                playParam: playParam,
                videoUrl: path
            },
            reload: true
        });
        return;
        var videoPlayer = api.require('videoPlayer');
        videoPlayer.play({
            path: path,
            texts: {
                head: {
                    title: title
                }
            },
            styles: {
                head: {
                    backSize: 24,
                    backImg: 'widget://image/vp_back.png',
                    setSize: 24,
                    setImg: 'widget://image/vp_more.png'
                },
                foot: {
                    playSize: 24,
                    playImg: 'widget://image/vp_play.png',
                    pauseImg: 'widget://image/vp_pause.png',
                    nextSize: 24,
                    nextImg: 'widget://image/vp_next.png'
                }
            }
        }, function (ret, err) {
            study_courseTimeRecordFun(playParam, 'NONE', 'true');
        });
    }

    function setupCourseStudyStatus(cid) {
        var allChapterSql = "SELECT timenum FROM t_study_course_record WHERE cid=" + cid + " AND uid=" + uid + " AND types=1;";
        selectSQL(allChapterSql, function (ret, err) {
            if (ret.status) {
                var chapterTimenums = ret.data;
                if (chapterTimenums.constructor == Array) {
                    var alltime = 0;
                    for (var i = 0; i < chapterTimenums.length; i++) {
                        var studiedTime = chapterTimenums[i].timenum;
                        if (studiedTime == '' || studiedTime == undefined || studiedTime == null || studiedTime == NaN || studiedTime == 'NaN') {
                            studiedTime = 0;
                        }
                        alltime += parseInt(studiedTime);
                    }
                    var courseRecordSql = "SELECT origtime FROM t_study_course_record WHERE vid=0 AND cid=" + cid + " AND uid=" + uid + ";";
                    selectSQL(courseRecordSql, function (ret, err) {
                        if (ret.status) {
                            //课程学习时间
                            var courseStudyTime = parseInt(ret.data[0].origtime);
                            if (alltime >= courseStudyTime) {

                                api.sendEvent({
                                    name: 'allChapterStuiedNotifi',//所有章节学完通知
                                    extra: {cid: cid}
                                });
                            }
                        }
                    });
                    var courseTimeSql = "SELECT alltime FROM t_study_course_info WHERE courseid=" + cid + ";";
                    selectSQL(courseTimeSql, function (ret, err) {
                        if (ret.status) {
                            var ctime = parseFloat(ret.data[0].alltime);
                            var schedule = Math.ceil((parseFloat(alltime) / ctime) * 100);
                            if (schedule > 100) {
                                schedule = 100;
                            }
                            api.sendEvent({
                                name: 'refreshCourseSchedule',
                                extra: {
                                    schedule: schedule,
                                    courseid: cid,
                                    pid: api.pageParam.pid
                                }
                            });
                        }
                    });
                    var examSql = "SELECT examid FROM t_study_exam WHERE courseid=" + cid + +" AND uid=" + uid + ";";
                    selectSQL(examSql, function (ret, err) {
                        var submitstatusStr = ", submitstatus=2";
                        if (ret.status) {
                            if (ret.data != null && ret.data != '' && ret.data != undefined) {
                                if (ret.examid != '' && ret.examid != 0) {
                                    submitstatusStr = "";
                                }
                            }
                        }
                        // var courseSql = "UPDATE t_study_course_record SET timenum=" + alltime + submitstatusStr + " WHERE vid=0 AND cid=" + cid + " AND crid=" + crid + " AND uid=" + uid + ";";
                        var courseSql = "UPDATE t_study_course_record SET timenum=" + alltime + submitstatusStr + " WHERE vid=0 AND cid=" + cid + " AND uid=" + uid + ";";
                        executeSQL(courseSql, function (ret, err) {
                            api.sendEvent({
                                name: 'uploadCourseRecordNotifi'
                            });
                        });
                    });
                }
            }
        });
    }

    function setRichtextsDownloadStatus() {
        openDB(function (ret, err) {
            for (var i = 0; i < richtextArr.length; i++) {
                var vid = richtextArr[i].id;
                setRichtextDB(vid);
            }
            //假下载
            // for (var i = 0; i < testArr.length; i++) {
            // 	var vid = testArr[i].vid;
            // 	setRichtextDB(vid);
            // }
        });
    }

    function setRichtextDB(vid) {
        var sql = "UPDATE t_study_chapter SET localpath = 'cache' WHERE chapter_id = '" + vid + "';";
        executeSQL(sql, function (ret, err) {
            if (ret.status) {
                $('#' + vid).find('.download_icon').addClass('downloaded');
            }
        });
    }

    function cancelDownloadTestAndRich() {
        openDB(function (ret, err) {
            if (ret.status) {
                for (var i = 0; i < testArr.length; i++) {
                    var vid = testArr[i].vid;
                    cancelDownloadStatus(vid);
                }
                for (var i = 0; i < richtextArr.length; i++) {
                    var vid = richtextArr[i].id;
                    cancelDownloadStatus(vid);
                }
            }
        })
    }

    function cancelDownloadStatus(vid) {
        var sql = "UPDATE t_study_chapter SET localpath = null WHERE chapter_id = '" + vid + "';";
        executeSQL(sql, function (ret, err) {
        });
    }

    function toHourMinute(minutes, seconds) {
        return (minutes.length === 1 ? "0" + minutes : minutes) + ":" + (seconds.length === 1 ? "0" + seconds : seconds);
        // var hour = Math.floor(minutes / 60);
        // if (hour < 10) {
        //     hour = '0' + hour;
        // }
        // var minute = minutes % 60;
        // if (minute < 10) {
        //     minute = '0' + minute;
        // }
        // var hourAndMinute = hour + ':' + minute;
        // if (hourAndMinute == '00:00') {
        //     hourAndMinute = '';
        // }
        // return hourAndMinute;
        // 也可以转换为json，以方便外部使用
        // return {hour:Math.floor(minutes/60),minute:(minutes%60)};
    }

    function htmlStringBuilder(chapter, chapterClassIndex, chapterSectionIndex) {

// 		 alert(JSON.stringify(chapter)+'\n'+chapterClassIndex+'\n'+chapterSectionIndex);
        var contentType = chapter.content_type;
        var chapterClassName = 'chapter' + chapterClassIndex;
        var id = chapter.chapter_id;
        var cid = chapter.cid;
        var rid = chapter.rid;
        var pid = chapter.pid;
        var other = chapter.other;
        var ctime = chapter.ctime;
        var etime = toHourMinute(chapter.ctime + "", chapter.mtime + "");
        var title = decodeURIComponent(chapter.cdetailtitle);
        var isclose = chapter.isclose;
        var bewrite = chapter.bewrite;
        var is_dis_bewrite = chapter.is_dis_bewrite;
        var hdresourceType = chapter.hdresourceType;
        if (contentType != 'tag' && contentType != 'part') {
            if (isclose == '0') {
                return "";
            }
        }
        if (contentType == 'tag') {//章
            return "<p id='" + id + "' cid='" + cid + "' class='weui_panel_hd list_section_contnt' value='" + chapterClassName + "'>" + title + "</p>";
        } else if (contentType == 'part') {//节
            return "<div class='weui_cell part-cell " + chapterClassName + "'>" +
                "<div class='weui_cell_bd weui_cell_primary'>" +
                "<p id='" + id + "' cid='" + cid + "' class='part-title'>" + title + "</p>" +
                "</div>" +
                "</div>";
            // return "<p id='" + id + "' cid='" + cid + "' class='weui_cells_title " + chapterClassName + "'>" + "第" + chapterSectionIndex + "节 " + title + "</p>";
        } else if (contentType == 'catetag') {
            return "<div style='margin-left: 10px' class='weui_cell tag-cell " + chapterClassName + "'>" +
                "<div class='weui_cell_bd weui_cell_primary'>" +
                "<p style='color: #F8575D;' id='" + id + "' cid='" + cid + "' class='part-title'>" + title + "</p>" +
                "</div>" +
                "</div>";
        } else if (contentType == 'doc') {//文档
            chapterArr.push({//如果可下载文档则打开即可
                id: id,
                cid: cid,
                rid: rid,
                pid: pid,
                ctime: ctime
            });
            allChapters.push({
                id: id
            });
            return "<div id='" + id + "' title='" + title + "' isclose='" + isclose + "' ctime='" + ctime + "' is_encr='" + chapter.is_encr + "' baidu_cloud_id='" + chapter.baidu_cloud_id + "' baidu_cloud_id_url='" + chapter.baidu_cloud_id_url + "' pid='" + pid + "' rid='" + rid + "' cid='" + cid + "' class='chapter_content_cell weui_cell doc_cell " + chapterClassName + "'>" +
                "<p class='chapter_cell_icon doc_unstudy' alt=''><p>" +
                "<div class='download_progress'></div>" +
                "<div class='weui_cell_hd'><img src=''></div>" +
                "<div class='weui_cell_bd weui_cell_primary'>" +
                "<label class='chapter_title'>" + title + "</label>" +
                "</div>" +
                // "<span class='chapter_etime'>" + etime + "</span>" +
                "<span class='weui_cell_ft chapter_cell_icon download_icon'></span>" +
                "</div>";
        } else if (contentType == 'video') {
            chapterArr.push({
                id: id,
                cid: cid,
                rid: rid,
                pid: pid,
                ctime: ctime
            });
            allChapters.push({
                id: id
            });
            return "<div id='" + id + "' title='" + title + "' etime='" + chapter.etime + "' is_encr='" + chapter.is_encr + "' baidu_cloud_id='" + chapter.baidu_cloud_id + "' baidu_cloud_id_url='" + chapter.baidu_cloud_id_url + "' isclose='" + isclose + "' ctime='" + ctime + "' pid='" + pid + "' rid='" + rid + "' sourceUrl='" + chapter.sourceUrl + "' cid='" + cid + "' class='chapter_content_cell weui_cell video_cell " + chapterClassName + "'>" +
                "<p class='chapter_cell_icon video_unstudy' alt=''><p>" +
                "<div class='download_progress'></div>" +
                "<div class='weui_cell_hd'><img src=''></div>" +
                "<div class='weui_cell_bd weui_cell_primary'>" +
                "<label class='chapter_title'>" + title + "</label>" +
                "</div>" +
                "<span class='chapter_etime'>" + etime + "</span>" +
                "<span class='weui_cell_ft chapter_cell_icon download_icon'></span>" +
                "</div>";
        } else if (contentType == 'richtext') {
            richtextArr.push({
                id: id
            });
            allChapters.push({
                id: id
            });
            return "<div id='" + id + "' bewrite='" + bewrite + "' title='" + title + "' isclose='" + isclose + "' ctime='" + ctime + "' pid='" + pid + "' other='" + other + "' cid='" + cid + "' class='chapter_content_cell weui_cell richtext_cell " + chapterClassName + "'>" +
                "<p class='chapter_cell_icon richtext_unstudy' alt=''><p>" +
                "<div class='download_progress'></div>" +
                "<div class='weui_cell_hd'><img src=''></div>" +
                "<div class='weui_cell_bd weui_cell_primary'>" +
                "<label class='chapter_title'>" + title + "</label>" +
                "</div>" +
                // "<span class='chapter_etime'>" + etime + "</span>" +
                "<span class='weui_cell_ft chapter_cell_icon download_icon'></span>" +
                "</div>";
        } else if (contentType == 'testsxxxxxxxxx') {//应为tests 测验暂时屏蔽
            var uid = uid;
            api.ajax({
                url: Study_CheckIsTest,
                method: 'post',
                timeout: 30,
                dataType: 'json',
                returnAll: false,
                data: {
                    values: {
                        pid: pid,
                        chapterid: id,
                        uid: uid
                    }
                }

            }, function (ret, err) {
                if (ret) {
                    if (ret.status == '1') {
                        if (ret.isanswer == '1') {
                            $('#' + id).children('p:first').addClass('tests_tested');
                        }
                        openDB(function (ret, err) {
                            var uniquekey = uid + id;
                            var isanswer = ret.isanswer;
                            var testParam = {
                                pid: pid,
                                chapterid: id,
                                uid: uid,
                                testid: rid,
                                isanswer: isanswer,
                                uniquekey: uniquekey
                            };
                            if (ret.status) {
                                insertData('t_study_test_record', testParam, function (ret, err) {
                                    if (ret.status) {

                                    } else {
                                        var updateTestSql = "UPDATE t_study_test_record SET isanswer=" + isanswer + " WHERE chapterid=" + id + " AND uid=" + uid + ";";
                                        executeSQL(updateTestSql, function (ret, err) {

                                        });
                                    }
                                });
                            }
                        });
                    } else {
                        openDB(function (ret, err) {
                            var uniquekey = uid + id;
                            var testParam = {
                                pid: pid,
                                chapterid: id,
                                uid: uid,
                                testid: rid,
                                uniquekey: uniquekey
                            };
                            if (ret.status) {
                                insertData('t_study_test_record', testParam, function (ret, err) {
                                });
                            }
                        });
                    }
                } else {
                    openDB(function (ret, err) {
                        if (ret.status) {
                            var selectTestSql = "SELECT isanswer FROM t_study_test_record WHERE chapterid=" + id + " AND uid=" + uid + ";";
                            selectSQL(selectTestSql, function (ret, err) {
                                if (ret.data.constructor == Array && ret.data.length > 0) {
                                    if (ret.data[0].isanswer == 1) {
                                        setTimeout(function () {//防止还没加载就设置属性
                                            $('#' + id).children('p:first').addClass('tests_tested');
                                        }, 500);
                                    }
                                }
                            });
                        }
                    });
                }
                ;
            });
            testArr.push({
                vid: id,
                cid: cid,
                pid: pid,
                testid: rid
            });
            return "<div id='" + id + "' title='" + title + "'  ctime='" + ctime + "' pid='" + pid + "' rid='" + rid + "' cid='" + cid + "' class='chapter_content_cell weui_cell tests_cell " + chapterClassName + "'>" +
                "<p class='chapter_cell_icon tests_untest' alt=''><p>" +
                "<div class='download_progress'></div>" +
                "<div class='weui_cell_hd'><img src=''></div>" +
                "<div class='weui_cell_bd weui_cell_primary'>" +
                "<label class='chapter_title'>" + title + "</label>" +
                "</div>" +
                "<span class='weui_cell_ft chapter_cell_icon download_icon'></span>" +
                "</div>";
        } else if (contentType == 'tests') {

            return "<div id='" + id + "' is_dis_bewrite='" + is_dis_bewrite + "' bewrite='" + bewrite + "' title='" + title + "' isclose='" + isclose + "' ctime='" + ctime + "' pid='" + pid + "' rid='" + rid + "' cid='" + cid + "' class='chapter_content_cell weui_cell tests_cell " + chapterClassName + "'>" +
                "<p class='chapter_cell_icon tests_untest' alt=''><p>" +
                "<div class='download_progress'></div>" +
                "<div class='weui_cell_hd'><img src=''></div>" +
                "<div class='weui_cell_bd weui_cell_primary'>" +
                "<label class='chapter_title'>" + title + "</label>" +
                "</div>" +
                "<span class='weui_cell_ft chapter_cell_icon download_icon'></span>" +
                "</div>";
        } else if (contentType == 'discuss') {
            return "<div id='" + id + "' bewrite='" + bewrite + "' title='" + title + "' isclose='" + isclose + "'  ctime='" + ctime + "' pid='" + pid + "' rid='" + rid + "' cid='" + cid + "' class='chapter_content_cell weui_cell discuss_cell " + chapterClassName + "'>" +
                "<p class='chapter_cell_icon taolun_normal' alt=''><p>" +
                "<div class='download_progress'></div>" +
                "<div class='weui_cell_hd'><img src=''></div>" +
                "<div class='weui_cell_bd weui_cell_primary'>" +
                "<label class='chapter_title'>" + title + "</label>" +
                "</div>" +
                "<span class='weui_cell_ft chapter_cell_icon download_icon'></span>" +
                "</div>";
        } else if (contentType == 'hdresource') {
            return "<div id='" + id + "' hdresourceType='" + hdresourceType + "' bewrite='" + bewrite + "' title='" + title + "' isclose='" + isclose + "'  ctime='" + ctime + "' pid='" + pid + "' rid='" + rid + "' cid='" + cid + "' class='chapter_content_cell weui_cell ziyuan_cell " + chapterClassName + "'>" +
                "<p class='chapter_cell_icon ziyuan_unstudy' alt=''><p>" +
                "<div class='download_progress'></div>" +
                "<div class='weui_cell_hd'><img src=''></div>" +
                "<div class='weui_cell_bd weui_cell_primary'>" +
                "<label class='chapter_title'>" + title + "</label>" +
                "</div>" +
                "<span class='weui_cell_ft chapter_cell_icon download_icon'></span>" +
                "</div>";
            // return'';
        } else if (contentType == 'works') {
            return "<div id='" + id + "' is_dis_bewrite='" + is_dis_bewrite + "' bewrite='" + bewrite + "' title='" + title + "' isclose='" + isclose + "' ctime='" + ctime + "' pid='" + pid + "' rid='" + rid + "' cid='" + cid + "' class='chapter_content_cell weui_cell works_cell " + chapterClassName + "'>" +
                "<p class='chapter_cell_icon tests_untest' alt=''><p>" +
                "<div class='download_progress'></div>" +
                "<div class='weui_cell_hd'><img src=''></div>" +
                "<div class='weui_cell_bd weui_cell_primary'>" +
                "<label class='chapter_title'>" + title + "</label>" +
                "</div>" +
                "<span class='weui_cell_ft chapter_cell_icon download_icon'></span>" +
                "</div>";
        } else {
            return '';
        }
    }

    function prepareData() {
        uid = $api.getStorage('cfnetppuid');
        jid = $api.getStorage('cfnetppjxid');
    }

    function getCourseUrl(id, videoObj) {
        cfnetppPOST(url_getCourseUrl, {"vid": id}, false, function (data, status) {
            if(data.status){
                openVideo(data.data.baidu_cloud_id_url, data.data.sourceUrl, videoObj);
            }
        })
    }

    function openVideo(baidu_cloud_id_url, sourceUrl, videoObj) {
        sendBuriedNotifi('1', 'mooc章节列表', '008', '');
        if (isSelected == 0) {//如果未选当前课程则不能学习
            api.toast({
                msg: '您还没有添加该课程',
                duration: 3000,
                location: 'bottom'
            });
            return;
        }
        if (isFreeCourse == false) {//付费课程未购买
            api.toast({
                msg: '该课程为付费课程，购买后才可学习',
                duration: 3000,
                location: 'bottom'
            });
            return;
        }
        var isclose = $(videoObj).attr('isclose');
        if (isclose == 0) {
            api.toast({
                msg: '该课时尚未发布，暂不能学习',
                duration: 3000,
                location: 'bottom'
            });
            return;
        }
        var vid = $(videoObj).attr('id');
        var cid = $(videoObj).attr('cid');
        var rid = $(videoObj).attr('rid');
        var pid = $(videoObj).attr('pid');

        var ctime = $(videoObj).attr('ctime');
        var title = $(videoObj).attr('title');
        var is_encr = $(videoObj).attr('is_encr');
        currentParam = {
            cid: cid,
            pid: pid,
            vid: vid,
        };
        if (sourceUrl != null && sourceUrl != '' && sourceUrl.indexOf("http://") >= 0) {
            rid = sourceUrl;
        }
        // if (vid != allChapters[0].id && checks == 1) {
        // 	for (var i = 0; i < allChapters.length; i++) {
        // 		if (vid == allChapters[i].id) {
        // 			prevVid = allChapters[i - 1].id;
        // 			var chapterID = '#' + prevVid;
        // 			// alert(i+'wwwww'+prevVid);
        // 			var chapterPrevDiv = $(chapterID);
        // 			if (chapterPrevDiv.hasClass('chapter_content_cell')) {
        // 				var chapterPrevDivP = chapterPrevDiv.children("p.chapter_cell_icon");
        // 				if (!chapterPrevDivP.hasClass('doc_studied') && !chapterPrevDivP.hasClass('video_studied') && !chapterPrevDivP.hasClass('richtext_studied')) {
        // 					api.toast({
        // 					    msg: '您还有未学完的课程，请学完后再开始学习下一章节',
        // 					    duration: 5000,
        // 					    location: 'bottom'
        // 					});
        // 					return;
        // 				}
        // 			}
        // 		}
        // 	}
        // }

        openDB(function (ret, err) {
            // if (ret.status) {
            var sql = "SELECT timenum,status FROM t_study_course_record WHERE vid = " + vid + " and uid = " + uid + ";";
            selectSQL(sql, function (ret, err) {
//						 alert(JSON.stringify(ret));
                // if (ret.status && ret.data != null && ret.data != undefined && ret.data != '') {
                var etime = '0';
                var timenum = ret.data.timenum;
                if (ret.status && timenum != null && timenum != undefined && timenum != '') {

                    etime = ret.data[0].timenum;
                }
                if (etime == null || etime == undefined || etime == '') {

                    etime = '0';
                }


                var status = ret.data[0].status;
                var localpathSql = "SELECT localpath ,rid FROM t_study_chapter WHERE chapter_id = '" + vid + "';";
                selectSQL(localpathSql, function (ret, err) {
                    // alert('路径参数' + JSON.stringify(ret));
                    if (ret.status && ret.data != null && ret.data != undefined && ret.data != '') {
                        var localpath = ret.data[0].localpath;
                        var other = ret.data[0].rid;

                        if (localpath != null && localpath != undefined && localpath != '') {
                            rid = localpath;
                        }
                    }

                    var type_url = rid.indexOf("http");

                    if (type_url > -1) {

                        var connectionType = api.connectionType + '';
//								    	alert("视频"+connectionType);
                        if (connectionType == 'none') {
                            api.toast({
                                msg: '网络异常，请检查网络',
                                duration: 2000,
                                location: 'bottom'
                            });
                            return;
                        }
                    }

                    var studyParam = {
                        videourl: rid,
                        imgurl: chapterParam.imgurl,
                        videoname: title,
                        vid: vid,
                        cid: cid,
                        pid: pid,
                        ctime: ctime,
                        etime: etime,
                        status: status,
                        cstatus: chapterParam.cstatus,
                        checks: api.pageParam.courseChecks,
                        crid: crid,
                        examid: chapterParam.examid,
                        allChapters: allChapters,
                        sourceurl: window.location.pathname,
                        sourcetitle: document.title,
                    };
//							     alert('章节参数' + JSON.stringify(studyParam));
                    // api.openWin({useWKWebView:true,
                    //     name: 'study_videoPlay',
                    //     url: 'study_videoPlay.html',
                    //     bounces: false,
                    //     slidBackEnabled : false,
                    //     pageParam: studyParam
                    // });
                    study_courseTimeRecordFun(studyParam, 'NONE', 'false');
                    var connectionType = api.connectionType;
                    if (connectionType != 'wifi' && connectionType != 'none') {
                        api.confirm({
                            title: '提示',
                            msg: '您当前处于移动数据网络下，是否继续播放',
                            buttons: ['确定', '取消']
                        }, function (ret, err) {
                            var index = ret.buttonIndex;
                            if (index == 1) {//确定

                                toBaiDuPlayer(is_encr, baidu_cloud_id_url, rid, title, studyParam);
                            }
                        });
                    } else {
                        toBaiDuPlayer(is_encr, baidu_cloud_id_url, rid, title, studyParam);
                    }
                });


            });
            // }
        });
        //设置学习状态
        var chapterID = '#' + vid;
        $(chapterID).children('p:first').addClass('video_studying');
        startStudying(vid);
    }
</script>
</html>
